package LeetCode.exercise;

public class LC38 {
    public static void main(String[] args) {
        System.out.println(countAndSay(1));
        System.out.println(countAndSay(2));
        System.out.println(countAndSay(3));
        System.out.println(countAndSay(4));
    }
    public static String countAndSay(int n) {
        String[] arr = new String[n];
        arr[0] = "1";
        for(int i = 1; i < arr.length; i++){
            StringBuilder sb = new StringBuilder();
            for(int left = 0, right = 0; right < arr[i-1].length(); left = right){
                char tmp = arr[i-1].charAt(left);
                while(right < arr[i-1].length() && arr[i-1].charAt(right) == tmp){
                    right++;
                }
                int count = right - left;
                sb.append(count);
                sb.append(tmp);

            }
            arr[i] = sb.toString();
        }
        return arr[n-1];
    }
}
